/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx;

import wtkx.collections.ArrayList;
import wtkx.collections.Sequence;
import wtkx.wtk.content.MenuItemDataDecorator;

/**
 * A {@link Menu} section.  A section is a grouping of menu items
 * within a menu.
 *
 * @author gbrown
 */
public class MenuSection 
    extends ArrayList<MenuItem>
{


    Menu menu = null;


    public MenuSection(){
        super();
    }


    public Menu getMenu() {
        return menu;
    }
    public void setMenu(Menu menu) {
        this.menu = menu;
    }
    public void insert(MenuItem item, int index) {
        if (item.getSection() != null) {
            throw new IllegalArgumentException("item already has a section.");
        }
        else {
            super.insert(item, index);
            item.setSection(this);

            if (menu != null) {
                menu.add(item);
                menu.getMenuListeners().itemInserted(this, index);
            }
        }
    }
    public int remove(MenuItem item) {
        int index = super.indexOf(item);
        if (index != -1) {
            this.remove(index, 1);
        }
        return index;
    }
    public Sequence<MenuItem> remove(int index, int count) {
        Sequence<MenuItem> removed = super.remove(index, count);

        for (int i = 0, n = removed.getLength(); i < n; i++) {
            MenuItem item = removed.get(i);

            item.setSection(null);

            if (menu != null) {
                menu.remove(item);
            }
        }

        if (menu != null) {
            menu.getMenuListeners().itemsRemoved(this, index, removed);
        }

        return removed;
    }

}
